package one.stand.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jmp.api.admin.dto.IndexDataRequest;
import com.jmp.api.admin.dto.IndexDataResponse;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import one.stand.mapper.AdminRecordMapper;
import one.stand.mapper.CompanyApiAdminMapper;
import one.stand.model.AdminRecordModel;
import one.stand.model.FinanceModel;
import one.stand.model.ResultModel;
import one.stand.request.AdminRecordRequest;
import one.stand.request.FinanceRequest;
import one.stand.util.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @author: ZRH
 * @CreateDate: 2019/4/17 9:45
 */
@Api(description = "PC首页")
@RestController
@RequestMapping("adminApi/index")
@Slf4j
public class IndexController extends BaseController {
    @Autowired
    private AdminRecordMapper adminRecordMapper;
    @Autowired
    private CompanyApiAdminMapper companyApiAdminMapper;

    @ApiOperation(value = "企业首页数据统计")
    @PostMapping("indexData")
    @SecurityAuthCheck
    public ResultModel<IndexDataResponse> indexData(IndexDataRequest request) {
        String stime = request.getStartTime();
        String etime = request.getEndTime();
        if (Objects.nonNull(request.getDays())) {
            Date now = new Date();
            stime = DateUtil.format(DateUtil.format, DateUtil.getDate(now, Calendar.DAY_OF_MONTH, -request.getDays()));
            etime = DateUtil.format(DateUtil.format, now);
        }
        etime = DateUtil.endDate(etime);
        IndexDataResponse response = companyApiAdminMapper.selectIndexDataCompany(stime, etime);
        IndexDataResponse ticketOrderSum = companyApiAdminMapper.selectTicketOrderSum();
        response.setOrderNum(ticketOrderSum.getOrderNum());
        response.setOrderMoney(ticketOrderSum.getOrderMoney());
        response.setAllCustomerCount(companyApiAdminMapper.selectIndexDataUser(null, null));
        response.setNewCustomerCount(companyApiAdminMapper.selectIndexDataUser(stime, etime));
        return ResultModel.success(response);
    }

    @ApiOperation(value = "查询管理员操作记录列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminId", value = "管理者id", paramType = "Integer"),
            @ApiImplicitParam(name = "company_id", value = "公司id", paramType = "String"),
            @ApiImplicitParam(name = "name", value = "员工名称、公司名称", paramType = "String"),
    })
    @PostMapping("selectAdminRecordList")
    @SecurityAuthCheck
    public ResultModel<Map<String, Object>> selectAdminRecordList(AdminRecordRequest request) {
        log.info("查询管理员操作记录列表：{}", JSON.toJSONString(request));
        AdminRecordModel model = new AdminRecordModel();
        if (request != null) {
            BeanUtils.copyProperties(request, model);
        }
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<AdminRecordModel> list = adminRecordMapper.select(model);
        if (list == null || list.size() <= 0) {
            return ResultModel.noData();
        }
        PageInfo<AdminRecordModel> pageInfo = new PageInfo(list);
        Map<String, Object> map = new HashMap<>(1);
        map.put("pageInfo", pageInfo);
        return ResultModel.success(map);
    }

    @ApiOperation(value = "企业首页数据统计折线图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "days", value = "天数(7,15,30),固定这三个", paramType = "Integer"),
    })
    @PostMapping("newCountList")
    @SecurityAuthCheck
    public ResultModel<Map<String, List<Map<String, Object>>>> newCountList(FinanceRequest request) {
        log.info("企业首页数据统计折线图参数：{}", JSON.toJSONString(request));
        FinanceModel model = new FinanceModel();
        if (request != null) {
            BeanUtils.copyProperties(request, model);
        }
        List<Map<String, Object>> newCustomerList = companyApiAdminMapper.newCustomerList(model);
        List<Map<String, Object>> newCompanyList = companyApiAdminMapper.newCompanyList(model);
        Map<String, List<Map<String, Object>>> map = new HashMap<>(2);
        map.put("newCustomerList", newCustomerList);
        map.put("newCompanyList", newCompanyList);
        return ResultModel.success(map);
    }

    @ApiOperation(value = "数据中心-数据概况")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyType", value = "1名片端   2企业端", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "days", value = "天数", paramType = "Integer"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "String"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "String"),
    })
    @PostMapping("dataCenter")
    @SecurityAuthCheck
    public ResultModel<Map<String, Object>> dataCenter(FinanceRequest request) {
        log.info("数据中心-数据概况参数：{}", JSON.toJSONString(request));
        FinanceModel model = new FinanceModel();
        if (request != null) {
            BeanUtils.copyProperties(request, model);
        }
        Map<String, Object> map = companyApiAdminMapper.dataCenter(model);
        return ResultModel.success(map);
    }

    @ApiOperation(value = "数据中心-用户增长情况")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyType", value = "1名片端   2企业端", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "days", value = "天数", paramType = "Integer"),
    })
    @PostMapping("newUserCountList")
    @SecurityAuthCheck
    public ResultModel<List<Map<String, Object>>> newUserCountList(FinanceRequest request) {
        log.info("数据中心-用户增长情况参数：{}", JSON.toJSONString(request));
        FinanceModel model = new FinanceModel();
        if (request != null) {
            BeanUtils.copyProperties(request, model);
        }
        List<Map<String, Object>> list = companyApiAdminMapper.newUserCountList(model);
        return ResultModel.success(list);
    }

    @ApiOperation(value = "数据中心-使用人数增长情况")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyType", value = "1名片端   2企业端", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "days", value = "天数", paramType = "Integer"),
    })
    @PostMapping("userUseCountList")
    @SecurityAuthCheck
    public ResultModel<List<Map<String, Object>>> userUseCountList(FinanceRequest request) {
        log.info("数据中心-使用人数增长情况参数：{}", JSON.toJSONString(request));
        FinanceModel model = new FinanceModel();
        if (request != null) {
            BeanUtils.copyProperties(request, model);
        }
        List<Map<String, Object>> list = companyApiAdminMapper.userUseCountList(model);
        return ResultModel.success(list);
    }

    @ApiOperation(value = "用户画像-性别分布")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyType", value = "1名片端   2企业端", required = true, paramType = "Integer"),
    })
    @PostMapping("userSexCount")
    @SecurityAuthCheck
    public ResultModel<List<Map<String, Object>>> userSexCount(FinanceRequest request) {
        log.info("用户画像-性别分布：{}", JSON.toJSONString(request));
        FinanceModel model = new FinanceModel();
        if (request != null) {
            BeanUtils.copyProperties(request, model);
        }
        List<Map<String, Object>> map = companyApiAdminMapper.userSexCount(model);
        return ResultModel.success(map);
    }

}
